package com.hnkywl.sts.action.tjbb;


import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.tjbb.YwsfcService;
import com.hnkywl.sts.util.POIUtil;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;


import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * User: SDE09
 * Date: 13-9-26
 * Time: 下午12:45
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/tjbb/ywsfcbb")
public class YwsfcAction extends SimpleActionSupport {
    private YwsfcService ywsfcService;

    private Integer gsid;
    private Date begindate;
    private Date enddt;
    private Integer select_type;
    private String wzbh;
    private String wzdl;
    private String pm;
    private Integer startrow;
    private Integer endrow;
    private Integer dc_flag;
    private int page;
    private int rows;
    private GnczService gnczService;
    private List<Gncz> gnczList;
    private Integer wz_type;
    private Integer ck_id;
    private String pz;
    private Integer cxfw;
    private Long defaultJg;
    private String defaultJgMc;

    @Action("ywsfcList")
    public void ywsfcList() {
        if (begindate == null) {
            begindate = new Date();
            Calendar calendar = Calendar.getInstance();
            //得到月初
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
            begindate = calendar.getTime();
        }
        if (enddt == null) {
            enddt = new Date();
        }
        if (select_type == null) {
            select_type = 1;
        }
        if (wz_type == null) {
            wz_type = 1;
        }
        String json = ywsfcService.ywsfcList(gsid, begindate, enddt, wz_type,wzdl,ck_id, page, rows,select_type, dc_flag);
        renderJson(json);
    }

    @Action("ywsfcbb")
    public String ywsfcbb() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        Calendar calendar = Calendar.getInstance();
        //得到月初
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        begindate = calendar.getTime();
        /* //得到月末
      calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
      enddt = calendar.getTime();*/
        enddt = new Date();
       // cxfw = YgService.getCurrentYgCxfw();
        //如果查询范围为全部不带入初始值，如果不是全部只能查询到本公司或本部门
       /* if (cxfw != 0) {
            defaultJg = YgService.getCurrentYgZgGsId() == 1 ?
                    YgService.getCurrentYgGsId() : YgService.getCurrentYgZgGsId();
            defaultJgMc = YgService.getCurrentYgZgGsId() == 1 ?
                    YgService.getCurrentYgGs() : YgService.getCurrentYgZgGs();
        }*/
        return SUCCESS;
    }

    @Action("bqrkcg")
    public String bqrkcg() {
        try {
            pz=URLDecoder.decode(pz,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    @Action("bqrkcglist")
    public void bqrkcgList() {        //Integer gsid,Integer select_type,Integer ck_id,String wzdl, Date begindate, Date enddt
        try {
            pz=URLDecoder.decode(pz,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        renderText(ywsfcService.bqrkcg(gsid, select_type, ck_id, wz_type, pz, begindate, enddt));
    }


    //bqckck
    @Action("bqckck")
    public String bqckck() {
        try {
            pz=URLDecoder.decode(pz,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }
    @Action("bqckcklist")
    public void bqckcklist() {
        try {
            pz=URLDecoder.decode(pz,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        renderText(ywsfcService.bqckck(gsid, select_type, ck_id, wz_type,pz, begindate, enddt));
    }
    //bqrkrktz
    @Action("bqrkrktz")
    public String bqrkrktz() {
        try {
            pz=URLDecoder.decode(pz,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }
    @Action("bqrkrktzlist")
    public void bqrkrktzlist() {
        try {
            pz=URLDecoder.decode(pz,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        renderText(ywsfcService.bqrkrktz(gsid, select_type, ck_id, wz_type,pz, begindate, enddt));
    }

    //bqckcktz
    @Action("bqckcktz")
    public String bqckcktz() {
        try {
            pz=URLDecoder.decode(pz,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }
    @Action("bqckcktzlist")
    public void bqckcktzlist() {
        try {
            pz=URLDecoder.decode(pz,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        renderText(ywsfcService.bqckcktz(gsid, select_type, ck_id, wz_type,pz, begindate, enddt));
    }

    //bqrkzr
    @Action("bqrkzr")
    public String bqrkzr() {
        try {
            pz=URLDecoder.decode(pz,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }
    @Action("bqrkzrlist")
    public void bqrkzrlist() {
        try {
            pz=URLDecoder.decode(pz,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        renderText(ywsfcService.bqrkzr(gsid, select_type, ck_id, wz_type,pz, begindate, enddt));
    }

    //bqckzc
    @Action("bqckzc")
    public String bqckzc() {
        try {
            pz=URLDecoder.decode(pz,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }
    @Action("bqckzclist")
    public void bqckzclist() {
        try {
            pz=URLDecoder.decode(pz,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        renderText(ywsfcService.bqckzc(gsid, select_type, ck_id, wz_type,pz, begindate, enddt));
    }
    //bqcknbly
    @Action("bqcknbly")
    public String bqcknbly() {
        try {
            pz=URLDecoder.decode(pz,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }
    @Action("bqcknblylist")
    public void bqcknblylist() {
        try {
            pz=URLDecoder.decode(pz,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        renderText(ywsfcService.bqcknbly(gsid, select_type, ck_id, wz_type,pz, begindate, enddt));
    }

    //exportywsfc
    @Action("exportywsfc")
    public String exportywsfc(){
        if(wz_type==null){
            wz_type=1;
        }
        dc_flag=1;
        Map<String,Map> map = ywsfcService.ywsfcListExport(gsid, begindate,enddt,wz_type,wzdl,ck_id,select_type,dc_flag);
        List<Map> result = (List<Map>) map.get("rows");
        List<Map> footer = (List<Map>) map.get("footer");
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet1");
        POIUtil poiUtil = new POIUtil(wb);
        sheet.setDefaultRowHeightInPoints(21);
        sheet.setDefaultColumnWidth((short) 20);
        HSSFCellStyle headerStyle = poiUtil.createHeader3HSSFCellStyle(wb);
        poiUtil.fillRow(sheet, headerStyle, 0,"公司名称", "品种",  "期初", "本期入库采购","本期入库拨入","本期入库调整","本期出库销售",
                "本期出库拨出 ","本期出库调整","本期出库内部领用"," 期末 ");
        //gs_name	pm   code	qc	rk	zr	rktz	ck	zc	cktz	nbly	qm
        for (int i = 1; i < result.size() + 1; i++) {
            Map item = result.get(i - 1);
            poiUtil.fillRowWithScale(sheet,null,i,4,item.get("gs_name"),item.get("pz"),item.get("qc"),
                    item.get("rk"),item.get("zr"),item.get("rktz"),item.get("ck"),item.get("zc"),item.get("cktz"),item.get("nbly"),
                    item.get("qm"));
        }
        HSSFCellStyle sumStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setColor(HSSFFont.COLOR_RED);
        sumStyle.setFont(font);
        poiUtil.fillRowWithScale(sheet,sumStyle,result.size() + 1,4,"合计：","",
                footer.get(0).get("qc"),     footer.get(0).get("rk"),      footer.get(0).get("zr"),
                footer.get(0).get("rktz"),   footer.get(0).get("ck"),   footer.get(0).get("zc"),
                footer.get(0).get("cktz"),  footer.get(0).get("nbly"),    footer.get(0).get("qm"));
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("业务收发存.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }
    //=======================getter==========setter==========================================

    public Integer getCxfw() {
        return cxfw;
    }

    public void setCxfw(Integer cxfw) {
        this.cxfw = cxfw;
    }

    public String getDefaultJgMc() {
        return defaultJgMc;
    }

    public void setDefaultJgMc(String defaultJgMc) {
        this.defaultJgMc = defaultJgMc;
    }

    public Long getDefaultJg() {
        return defaultJg;
    }

    public void setDefaultJg(Long defaultJg) {
        this.defaultJg = defaultJg;
    }

    public String getPz() {
        return pz;
    }

    public void setPz(String pz) {
        this.pz = pz;
    }

    public Integer getWz_type() {
        return wz_type;
    }

    public void setWz_type(Integer wz_type) {
        this.wz_type = wz_type;
    }

    public Integer getCk_id() {
        return ck_id;
    }

    public void setCk_id(Integer ck_id) {
        this.ck_id = ck_id;
    }

    public String getWzdl() {
        return wzdl;
    }

    public void setWzdl(String wzdl) {
        this.wzdl = wzdl;
    }

    public Integer getGsid() {
        return gsid;
    }

    public void setGsid(Integer gsid) {
        this.gsid = gsid;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public Integer getDc_flag() {
        return dc_flag;
    }

    public void setDc_flag(Integer dc_flag) {
        this.dc_flag = dc_flag;
    }

    public Integer getEndrow() {
        return endrow;
    }

    public void setEndrow(Integer endrow) {
        this.endrow = endrow;
    }

    public Integer getStartrow() {
        return startrow;
    }

    public void setStartrow(Integer startrow) {
        this.startrow = startrow;
    }

    public String getPm() {
        return pm;
    }

    public void setPm(String pm) {
        this.pm = pm;
    }

    public String getWzbh() {
        return wzbh;
    }

    public void setWzbh(String wzbh) {
        this.wzbh = wzbh;
    }

    public Integer getSelect_type() {
        return select_type;
    }

    public void setSelect_type(Integer select_type) {
        this.select_type = select_type;
    }

    public Date getEnddt() {
        return enddt;
    }

    public void setEnddt(Date enddt) {
        this.enddt = enddt;
    }

    public Date getBegindate() {
        return begindate;
    }

    public void setBegindate(Date begindate) {
        this.begindate = begindate;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    @Autowired
    public void setYwsfcService(YwsfcService ywsfcService) {
        this.ywsfcService = ywsfcService;
    }
    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }
}
